package cn.ittaoli.ajax.controller;

import cn.ittaoli.ajax.model.*;
import cn.ittaoli.ajax.service.ArticleService;
import cn.ittaoli.ajax.service.SlideService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by lub_lenovo on 2017/8/10.
 */
@Controller
@RequestMapping("/phone/article")
public class PhoneArticleController {
    @Autowired
    private ArticleService articleService;

    @RequestMapping("/getByPage")
    @ResponseBody
    public Map<String, Object> getByPage(@RequestParam(defaultValue = "1") Integer page,@RequestParam(defaultValue = "5") Integer rowsPerPage,@RequestParam(defaultValue = "0")Integer navid){
        PageInfo<ArticleView> pageInfo = articleService.getByPagePhone(page, rowsPerPage,navid);
        Map<String, Object> result = new HashMap<String,Object>();
        result.put("totalPage",pageInfo.getPages());
        result.put("rows",pageInfo.getList());
        return result;
    }
    @RequestMapping("/getArticle")
    @ResponseBody
    public Map<String,Object> getArticle(Integer id){
        Map<String,Object> result=articleService.getArticle(id);
        return result;
    }

    /**
     * 测试递归
     * 1.取该篇文章的所有评论，一级评论
     * 2.遍历所有一级评论，查看每一条评论的回复内容
     * 3.遍历所有一级评论，查看每一条评论的回复内容
     */

    @RequestMapping("/comments")
    @ResponseBody
    public List<ArticleCommentTree> getAllComments(){
        List<ArticleCommentTree> articleCommentTrees=this.findComment(1,0);
        return articleCommentTrees;
    }


    //构建评论回复所有递归内容
    public List<ArticleCommentTree> findComment(Integer articleid,Integer replyId) {
        //取得所有的直接评论的信息，即:replyId=0
        List<ArticleCommentTree> comments = articleService.getAllComment(articleid,replyId);
        for(ArticleCommentTree comment : comments){
            List<ArticleCommentTree> replys = new ArrayList<ArticleCommentTree>(); // 实例化回复的集合
            comment.setChildren(replys); // 设置评论的回复集合
            buildReplyComment(articleid,comment, replys); // 构建评论与回复信息
        }
        return comments;
    }
    /**
     * 构建评论与回复评论的关系
     * @param comment
     */
    private void buildReplyComment(Integer articleid,ArticleCommentTree comment, List<ArticleCommentTree> replys){
        List<ArticleCommentTree> replyComments = articleService.getAllComment(articleid,comment.getId()); // 获取评论的所有回复
        replys.addAll(replyComments); // 把所有的回复添加到评论实例化的回复集合中

        for(ArticleCommentTree c : replyComments){ // 遍历回复中的回复
            List<ArticleCommentTree> replyss = new ArrayList<ArticleCommentTree>(); // 实例化回复的集合
            c.setChildren(replyss);
            buildReplyComment(articleid,c, replyss); // 递归调用
        }
    }
}
